<?php

class MemberController extends Controller {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules() {
        return array(
            array('deny', // allow all users to access 'index' and 'view' actions.
//                'actions' => array(''),
                'users' => array('@'),
            ),
        );
    }

    public function init() {
        $cs = Yii::app()->clientScript;
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/common.css');
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/v3/n_common.css');
        $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/common.js", CClientScript::POS_HEAD);
        $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/v3/n_common.js", CClientScript::POS_HEAD);
        $cs->registerCoreScript('jquery');
        $cs->registerCoreScript('jquery.ui');
        $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/jquery.placeholder.js", CClientScript::POS_HEAD);
    }

    /**
     * Declares class-based actions.
     */
    public function actions() {
        return array(
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    public function actionIndex() {
//        print_r(Yii::app()->user->info['email']);
//        $a = '{"id":34,"city":"1","userId":1,"area":"1","mobile":"13667112141","district":null,"postalcode":"430000","province":"1","recipients":"张何勃","street":"1","telephone":"027-85752857转1234","defaultAddress":1,"streetDetail":"范德萨范德萨"}';
//        print_r(CJSON::decode($a));
        $cs = Yii::app()->clientScript;
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/index.css');

        $row = @file_get_contents('http://' . Yii::app()->params['apiHostProduct'] . ':' . Yii::app()->params['apiPortProduct'] . '/ProductCenter/select?q=**&wt=json&indent=true');
        $row = json_decode($row, true);
        print_r($row);
        $this->render('index', array('model' => $row['results']));
    }

    public function actionError() {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

    public function actionGetPwdByEmail() {
        $this->layout = 'clearmain'; // 选择模板框架

        if (Yii::app()->user->isGuest) {
            $model = new GetpwdForm('getpwdbyemail');

            $flag['status'] = 0;
            $flag['email'] = '';
            $flag['types'] = 0;

            $cs = Yii::app()->clientScript;
            $cs->registerCssFile(Yii::app()->baseUrl . '/css/login.css');
            $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/zhbform.js", CClientScript::POS_HEAD);

            if (isset($_POST['GetpwdForm'])) {
                $model->attributes = $_POST['GetpwdForm'];
                if ($model->validate()) {
                    if ($model->getpwdbyemail()) {
                        $flag['status'] = 1;
                        $flag['email'] = $model->email;
                        $flag['types'] = 1;
                        
                        Yii::app()->session['getpwd'] = CJSON::encode($flag);
                        
                        $this->redirect(Yii::app()->params['homeUrl'] . '/getpwd/email/done');
                    }
                }
            }
            Yii::app()->session['getpwd'] = CJSON::encode($flag);

            $this->render('getpwdbyemail', array('model' => $model));
        } else {
            $this->redirect(Yii::app()->params['homeUrl']);
        }
    }

    public function actionGetPwdByMobile() {
        $this->layout = 'clearmain'; // 选择模板框架

        if (Yii::app()->user->isGuest) {
            $model = new GetpwdForm('getpwdbymobile');
            $flag['status'] = 0;
            $flag['mobile'] = '';
            $flag['uid'] = 0;
            $flag['types'] = 0;

            $cs = Yii::app()->clientScript;
            $cs->registerCssFile(Yii::app()->baseUrl . '/css/login.css');
            $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/zhbform.js", CClientScript::POS_HEAD);

            if (isset($_POST['GetpwdForm'])) {
                $model->attributes = $_POST['GetpwdForm'];
                if ($model->validate()) {
                    $flag['uid'] = $model->uid;
                    if ($model->getpwdbymobile()) {
                        $this->redirect(Yii::app()->params['homeUrl'] . '/getpwd/newpwd');
                    }
                }
            }
            Yii::app()->session['getpwd'] = CJSON::encode($flag);

            $this->render('getpwdbymobile', array('model' => $model, 'uid' => $flag['uid']));
        } else {
            $this->redirect(Yii::app()->params['homeUrl']);
        }
    }

    public function actionGetPwdByEmailDone() {
        $this->layout = 'clearmain';  // 选择模板框架
        $emailto = '';
        $cs = Yii::app()->clientScript;
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/login.css');

        $ckjson = CJSON::decode(Yii::app()->session['getpwd']);

        if ($ckjson['types'] == 1) {
            $commonclass = new CommonClass();
            $emailto = $commonclass->emailto($ckjson['email']);
        } else {
            $this->redirect(Yii::app()->homeUrl . 'getpwd/email');
        }

        $this->render('getpwdby_email_done', array('ckjson' => $ckjson, 'emailto' => $emailto));
    }

    public function actionNewPwd() {
        $this->layout = 'clearmain';  // 选择模板框架
        $emailto = '';
        $cs = Yii::app()->clientScript;
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/login.css');

        if ($_GET['types'] == 1) {
            $flag['status'] = 1;
            $flag['email'] = $_GET['email'];
            $flag['uid'] = $_GET['uid'];
            $flag['types'] = $_GET['types'];
            Yii::app()->session['getpwd'] = CJSON::encode($flag);
        }

        if (Yii::app()->user->isGuest) {
            $model = new NewPwdForm('newpwd');
            $ckjson = CJSON::decode(Yii::app()->session['getpwd']);

            if ($ckjson['types'] > 0) {
                if (isset($_POST['NewPwdForm'])) {
                    $model->attributes = $_POST['NewPwdForm'];
                    if ($model->validate()) {
                        if ($model->newpwd()) {
                            $this->redirect(Yii::app()->params['homeUrl'] . '/getpwd/done');
                        }
                    }
                }
            } else {
                $this->redirect(Yii::app()->params['homeUrl']);
            }

            $this->render('newpwd', array('model' => $model, 'ckjson' => $ckjson));
        } else {
            $this->redirect(Yii::app()->params['homeUrl']);
        }
    }
    
    public function actionNewPwdDone() {
        $this->layout = 'clearmain';  // 选择模板框架
        $cs = Yii::app()->clientScript;
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/login.css');

        if (Yii::app()->user->isGuest) {
            $this->render('newpwddone');
        } else {
            $this->redirect(Yii::app()->params['homeUrl']);
        }
    }

}
